package com.zhb.controller;

import com.zhb.Dto.Result;
import com.zhb.service.SMservice;
import com.zhb.untils.UpOrDown;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Description TODO
 * @Author Bigbo
 * @Date 2023/5/3 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("sm")
public class SMController {

    @Autowired
    SMservice sMservice;
    @Autowired
    UpOrDown upOrDown;


    String localPath = "F:\\Idea\\t-camp11\\file";
    @PostMapping("/sm4Encrypt")
    Result sm4Encrypt(String key ,String sm4plaintext) throws Exception {
        String replace = sm4plaintext.replace('\n',' ').replace('\r',' ');
        Result result = sMservice.sm4CbcEncrypt(replace, key);
        System.out.println(result);

        return result;
    }

    @PostMapping("/sm4CbcDecrypt")
    Result sm4CbcDecrypt(String key ,String sm4ciphertext) throws Exception {
        String replace = sm4ciphertext.replace('\n',' ').replace('\r',' ');
        Result result = sMservice.sm4CbcDecrypt(replace, key);
        System.out.println(result);
        return result;
    }

    @PostMapping("/sm3Dgst")
    Result sm3Dgst(String sm3plaintext) throws Exception {
        String replace = sm3plaintext.replace('\n',' ').replace('\r',' ');
        Result result = sMservice.sm3Dgst(replace);
        System.out.println(result);
        return result;
    }
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public HashMap upload(@RequestParam("file") MultipartFile file) {
        String localfilepath = "F:\\Idea\\t-camp11\\Tongsuo\\src\\main\\resources\\file"+file.getOriginalFilename();
        File file1 = new File(localfilepath);
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String uploadfile = upOrDown.Upload(localfilepath, file.getOriginalFilename());
        HashMap<String, String> result = new HashMap<>();
        result.put("path",uploadfile+".sign");
        return result;
    }

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public void downlod(String filePath) throws IOException {
        String[] split = filePath.split("/");
        upOrDown.download(localPath+ split[split.length-1],filePath);
        String filename=localPath+ split[split.length-1];
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        // 设置信息给客户端不解析
        String type = new MimetypesFileTypeMap().getContentType(filename);
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        response.setHeader("Content-type",type);
        // 设置编码
        String hehe = new String(filename.getBytes("utf-8"), "iso-8859-1");
        // 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
        response.setHeader("Content-Disposition", "attachment;filename=" + hehe);

        upOrDown.download2Http(filename,response);

        // upOrDown.download(localPath+ split[split.length-1],filePath);
        return ;
    }

    @PostMapping("/sm2Verify")
    Result sm2Verify(String filePath ,String signFilePath) throws Exception {
        Result result = sMservice.sm2Verify(signFilePath, filePath);
        System.out.println(result);

        return result;
    }

    @PostMapping("/sm2Sign")
    HashMap sm2Sign(String path) throws Exception {
        sMservice.sm2Sign(path+".sign", path);
        HashMap<String, String> result = new HashMap<>();
        result.put("path",path+".sign");
        return result;
    }




}
